 function  [bdraw,m1,CC1,numit]=drawnorm_sur(y,x,prm,prprec,siginv,flag_csta);
%function [bdraw,m1,CC1]=drawnorm(y,x,prm,prprec,sig); 
% Draw Coefficients from a Normal distribution when prior is also 
%      Normal( prm, prprec^(-1) )  and prprec is the precision
%      No restrictions imposed when sampling 
% 
% y      (Txn) 
% x      (TxK)  
% Prm    (Kx1)  Prior Mean 
% prprec (KxK)  Prior Precision
% siginv (nxn)  Variance of the errors
%
% bdraw  (1xK)  Draw from the posterior normal as a row vector 
% m1     (1xK)  Posterior mean 
% CC1    (KxK)  Cholelsky of the posterior variance 
%               V1= CC1'*CC1  
% 
% NOTE: For the sur model with stacked coefficients 
%       y [TN x1 ]  x [TN K]  
% siginv =[NT  NT] = kron( sig^(-1) , eye(T) ) 
 % ================================================================
nk=length(prm);
V1=( prprec + (x')*siginv*x  ) \ eye(nk) ;   % Posterior Variance  
m1=V1*(prprec*prm + (x')*siginv*y);          % Posterior Mean  
CC1=chol(V1); 

if flag_csta == 0 
    
    bdraw=m1'+ randn(1,nk)*CC1;
    m1=m1'; 
    
    numit = 0; 
    return 
    
else
    
    numit = 0;
    cont = 0; 
    while cont < 1; 
        bdraw=m1'+ randn(1,nk)*CC1;
        if max( abs(bdraw) ) < 0.999 
            cont = 1 ; 
        else
            numit = numit + 1; 
            if numit > 100 
                numit= 0; 
                warning('More than 100 draws could not generate stationarity') 
            end 
        end 
        
    end 

end 



% Chech the sta


